package ModelLayer;
import java.util.ArrayList;
public class AddressBook
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> persons;
    private static AddressBook instance;
    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        persons = new ArrayList<Person>();
    }
    
    /**
     * This method initialises the AddressBook if one doesn't allready exist.
     * 
     * @return The instance of AddressBook. As an AddressBook
     */
    public static AddressBook getInstance(){
        if(instance == null){
            instance = new AddressBook();
        }
        return instance;
    }
    
    /**
     * Create a new person and add this person to the list of persons.
     * @param String name - The name of the person
     * @param String address - The address of the person
     * @param String phoneNumber - The persons phone number
     * @param String email - The persons email
     */
    public void addPerson(Person person)
    {
            persons.add(person);
    }
    
    
    /**
     * Finds the person with the given ID
     * 
     * @param long id - The ID to search for
     * @return Person - The object of the type Person, that has the given ID
     */
    public Person findPersonById(long id){
        for(int i = 0; i<persons.size(); i++){
            if(persons.get(i).getId()==id){
                return persons.get(i);
            }
       }
          return null;
    }
    
    /**
     * Finds the person with the given name
     * 
     * @param String name - The name of the person wanted
     * @return Person - The object of the type Person, that has the given name
     */
    @SuppressWarnings("unused")
	private Person findPersonByName(String name){
        for(int i = 0; i<persons.size(); i++){
            if(persons.get(i).getName().equals(name)){
                return persons.get(i);
            }
        }
        return null;
    }
    
    /**
     * Finds the person with the given phone number
     * 
     * @param String phoneNumber - The phone number of the person wanted
     * @return Person - The object of the type Person, that has the given phone number
     */
    private Person findPersonByPhoneNumber(String phoneNumber){
        for(int i = 0; i<persons.size(); i++){
            if(persons.get(i).getPhoneNumber().equals(phoneNumber)){
                return persons.get(i);
            }
            
        }
        return null;
    }
    
    /**
     * Deletes the person(s) with the given ID
     * @param long id - The ID of the person to be deleted
     */
    public void deletePersonById(long id){
        Person p = findPersonById(id);
        if(p!=null){
            persons.remove(p);
        }
    }
    
    /**
     * Deletes the person(s) with the given phone number
     * @param String phoneNumber - The phone number of the person to be deleted
     */
    @SuppressWarnings("unused")
	private void deletePersonByPhoneNumber(String phoneNumber){
       Person p = findPersonByPhoneNumber(phoneNumber);
       if(p!=null){
           persons.remove(p);
        }
    }
    
    //     /**
    //      * Updates the information about a person in the collection
    //      * 
    //      * @param long id - the id of the person
    //      * @param String name - the name of the person
    //      * @param String address - the address of the person
    //      * @param String postalCode - the postalcode of the person
    //      * @param String city - the city the person lives in
    //      * @param String phoneNumber - the phonenumber of the person
    //      */
    //     public void updatePerson(long id, String name, String address, String postalCode, String city, String phoneNumber){
        //         if(findPersonById(id)!=null){
            //             findPersonById(id).updatePerson(name, address, phoneNumber, email);
            //         }
            //     }
            //Set & Get methods for the entire class
    /**
     * Returns a list of person in the collection
     * 
     * @return ArrayList persons - a list of persons
     */
    @SuppressWarnings("rawtypes")
	public ArrayList getPersonList(){
        return persons;
    }
    
    public ArrayList<String> getListOfCustomers()
    {
    	boolean isCustomer = true;
    	ArrayList<String> c = new ArrayList<String>();
    	String s = "";
    	for(Person p : persons)
    	{
    		s = "";
    		isCustomer = true;
            try
            {
                p = (Customer)p;
            }
            catch(Exception e)
            {
                isCustomer = false;
            }
    		s = "ID: " + p.getId() + "   Navn: " + p.getName() + "    Status: "; 
            if(isCustomer){
            	s = s + "Kunde";    		
            }
            else{
            	s = s + "Medarbejder";
            }
    		c.add(s);
    	}
    	return c;
    }
    
    
    
    /**
     * This method returns a list of all customers
     * 
     * @return  A list of customers.
     */
    public ArrayList<Customer> getCustomerList()
    {
        ArrayList<Customer> customerList = new ArrayList<Customer>();
        for(Person p : persons)
        {
            try
            {
                customerList.add((Customer)p);
            }
            catch(Exception e){}
        }
        return customerList;
    }
}